<?php
	include("../../connect/connect_mysql.php");
	include("../../js/uploadify/SimpleImage.php");

function check_file_type($filename){
	$upload_type = array('jpg', 'jpeg', 'gif', 'png');
    $start	= strrpos($filename, ".");
	if ( $start < strlen($filename) ){
		$user_type	= strtolower(substr($filename, $start+1));
		if ( in_array($user_type, $upload_type) ){
			return true;
		}
	}
	return false;
}
	$chk= array();
	$name_thumb = array();
	if(isset($_POST["image_gallery"])){
		$chk =$_POST["image_gallery"];
	}
	if(isset($_POST["image_collection"])){
		$chk =$_POST["image_collection"];
	}
	if(isset($_POST["image_menu"])){
		$chk =$_POST["image_menu"];
	}
	if(isset($_POST["image_event"])){
		$chk =$_POST["image_event"];
		
	}
	$count = count($chk);

	$ids ="";
	if ($count != 0){
		for ($i =0; $i < $count ; $i++){
			$ids .= $chk[$i] . ",";
		}
		
		$ids = chop($ids,",");
		$query = "SELECT thumb FROM image WHERE image_id IN ($ids)";
		mysql_query ("SET NAMES 'utf8'");
		$result  = mysql_query($query);
		$num = mysql_num_rows($result);
		while($row = mysql_fetch_array($result)){
			array_push($name_thumb, $row['thumb']);
		}
		if ($_FILES['uploadlogo']['name']!= "" && check_file_type($_FILES['uploadlogo']['name']) && ($_FILES['uploadlogo']['size']<102400)){
		$tempFile = $_FILES['uploadlogo']['tmp_name'];
		$targetPath = $_SERVER['DOCUMENT_ROOT'] .'/wheelhouse/upload/thumb/';
		$grayPath = $_SERVER['DOCUMENT_ROOT'] .'/wheelhouse/upload/gray/';

		$size = getimagesize($tempFile);
		$image = new SimpleImage();
		$image->load($tempFile);
		for($i=0;$i < count($chk);$i++){
			$targetFile =  str_replace('//','/',$targetPath) . $name_thumb[$i];
			$image->save($targetFile);
			$grayFile =  str_replace('//','/',$grayPath). $name_thumb[$i];
			$image -> grayscale($size[0],$size[1]);
			$image->save($grayFile);
		}
    	//echo $ids;
		$query ="UPDATE image SET flg_thumb = 1 WHERE image_id in ($ids)";
		mysql_query ("SET NAMES 'utf8'");
		mysql_query($query);
		// Uncomment the following line if you want to make the directory if it doesn't exist
		// mkdir(str_replace('//','/',$targetPath), 0755, true);
		}
	}

	echo "<script>window.location='/wheelhouse/admin/?act=gallery'</script>";
?>
